package com.tsing.cli.system.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import com.tsing.cli.common.exception.ResponseCode;
import com.tsing.cli.common.vo.ResultData;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import java.io.IOException;

/**
 * 授权异常处理器
 *
 * @author TheTsing
 */
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException {
        response.setContentType("application/json;charset=UTF-8");
        response.getWriter().write(JSON.toJSONString(ResultData.error(ResponseCode.FORBIDDEN.getCode(), ResponseCode.FORBIDDEN.getDescription()), JSONWriter.Feature.WriteNulls));
    }

}
